package com.fept.msgcore.msgserver.netty.server;

import com.fept.msgcore.msgserver.netty.base.IMessageServer;
import com.fept.msgcore.msgserver.netty.handler.ProtocolMessageDecoderFactory;
import com.fept.msgcore.msgserver.netty.handler.ProtocolMessageEncoderFactory;
import com.fept.msgcore.msgserver.netty.handler.ProtocolMessageHandlerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by Srma on 1/13/2017.
 */
public class MessageServer implements IMessageServer {

    private static Logger logger = LoggerFactory.getLogger(MessageServer.class);
    static final public String PROTOCOL_MESSAGE_SERVER = "msgcore-server";

    private NettyServer protocolMessageServer = null;
    private int port = 6429;

    /**
     *
     */
    public MessageServer() {
        this.start();
    }

    /**
     *
     * @param name
     * @param port
     */
    public MessageServer(int port) {
        this.port = port;
        this.start();
    }

    /**
     *
     */
    protected void start() {
        logger.info("Create protocol message server: start");
        protocolMessageServer = NettyServer.createNettyServer(
                PROTOCOL_MESSAGE_SERVER, this.port,
                new ProtocolMessageEncoderFactory(PROTOCOL_MESSAGE_SERVER),
                new ProtocolMessageDecoderFactory(PROTOCOL_MESSAGE_SERVER),
                new ProtocolMessageHandlerFactory(PROTOCOL_MESSAGE_SERVER));
        protocolMessageServer.start();

        logger.info("Create protocol message server: done");
    }
}
